//
//  SubDistrictController.m
//  YunGui
//
//  Created by Hanen 3G 01 on 16/3/22.
//  Copyright © 2016年 hanen. All rights reserved.
//

#import "SubDistrictController.h"
#import "ManagerNoticeBtnView.h"
#import "TopBtn.h"
#import "SubDsitrictCell.h"
#import "MJRefresh.h"
#import "ExpandTalkingVillageCell.h"
#import "SubDistrictDetailController.h"
#import "SubDistrictStatisticCell.h"
#import "Admin_City_CreatDistricViewController.h"
#import "AdministerDistrictCell.h"
#import "NetStatisticViewController.h"
#import "MIneDistrictModel.h"
#import "SideMenuUtil.h"
#import "TaiZhangManagerViewController.h"
#import "StatisticModel.h"
#import "MySegButtonView.h"
#import "SubDistrictDetailModel.h"
#import "SelectDistrictCell.h"

@interface SubDistrictController ()<ManagerNoticeBtnViewDelegate,UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
{
    ManagerNoticeBtnView *btnView;
    
    UITableView       *_tableView;
    
//    TopBtn   *_clickbtn;
    UIView  *_segmentBackView;
    UISegmentedControl *_segment;
//    NSString *_chooseParames;//选中竞争对手 还是 等级统计
    int flag;//选中竞争对手 1 还是 等级统计2
    
    MySegButtonView *segBtnView;
    
    UIButton  *_newSubdistrictBtn;
    UIButton *righB;
    
    int  role ;
    NSMutableArray *mineArray;
    NSMutableArray *expandArray;
    NSMutableArray *talkingArray;
    int  type;//区分点击的各个部分
    
    UITextField *_searchTF;
    UIView      *_searchView;
    UIButton  *_selectBtn;
    UIButton  *_selectListBtn;
    UIView * taxisView;
    
    
    NSInteger  pageNum1;
    NSInteger  pageNum2;
    NSInteger  pageNum3;
    
    
    int btnTag;
    BOOL isGet;
}

@end

@implementation SubDistrictController
@synthesize revealController;
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = GrayBackColor;
    
    self.navigationItem.title = @"小区管理";
    self.navigationController.navigationBar.backgroundColor = BlueColor;
    mineArray  = [NSMutableArray array];
    expandArray = [NSMutableArray array];
    talkingArray = [NSMutableArray array];
    pageNum1 = 1;
    pageNum2 = 1;
    pageNum3 = 1;
    [SideMenuUtil addViewGesture:self revealController:revealController];
    
    righB = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, [UIView getWidth:20], [UIView getWidth:20])];
    [righB setBackgroundImage:[UIImage imageNamed:@"search.png"] forState:UIControlStateNormal];
    [righB addTarget:self action:@selector(clickSearch:) forControlEvents:UIControlEventTouchUpInside];
    righB.selected = NO;
    UIBarButtonItem *barbtn = [[UIBarButtonItem alloc] initWithCustomView:righB];
    self.navigationItem.rightBarButtonItem = barbtn;
    
    int roleID = [[[NSUserDefaults standardUserDefaults] objectForKey:@"roleId"] intValue];
    //
    if (roleID == 4) {//商务
        role =0 ;
    }else if (roleID == 2){//城市总监
        role = 1 ;
    }else if(roleID == 1){//管理者
        role = 2;
    }

    //    role = 0; //0 是商务经理 1 城市总监 2管理者
    //    if (role == 0) {
    //        type = 1;
    //    }
    //    if (role == 1) {
    //        type = 1;//暂时现请求辖区数据 只有辖区小区的数据
    //    }
    //    if(role == 2){
    //        type = 1;
    //    }
    
    
    if(self.isChooseDistrict == NO){
        self.navigationItem.leftBarButtonItem=[ViewTool getListBarButtonItemWithTarget:self WithAction:@selector(sideLeftButton_selector:)];
        [self addTopBtnView];
        
        type = 1;
        
        if(role == 1 || role == 2){
            righB.hidden = YES;
            
            [self addSegment];
        }
        
    }else{
        self.navigationItem.leftBarButtonItem=[ViewTool getBackBarButtonItemWithTarget:self WithAction:@selector(goToBack)];
        self.navigationItem.title = @"选择小区";
        if (_isTongQian == NO) {//使用获取拓展小区的列表接口
        }else{
        }
        role = 0;
        type = 4;
        pageNum2 = 1;
        [expandArray removeAllObjects];
        [self loadDataWithSearchText:nil];
    }
    
    [self addTableview];
    [self addCreatbtn];
    [self createSearchView];
    
}
#pragma mark --顶上的按钮
- (void)addTopBtnView
{
   btnView = [[ManagerNoticeBtnView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 38)];
    if (role == 0) {//高级经理
         btnView.btnTitleArray = @[@"我的小区",@"公海小区"];
    }else if(role == 1){//城市总监
         btnView.btnTitleArray = @[@"小区统计",@"辖区小区",@"在谈小区"];
    }else if(role == 2){ //管理者
         btnView.btnTitleArray = @[@"小区统计",@"所有小区"];
    }
    btnView.backgroundColor = BlueColor;
    btnView.delegate = self;
    btnView.titleColor = [UIColor whiteColor];
    [self.view addSubview:btnView];
}

- (void)addSegment
{
    _segmentBackView = [[UIView alloc] initWithFrame:CGRectMake(0, btnView.maxY, kScreenWidth, 70.0f)];
    _segmentBackView.backgroundColor = UIColorFromRGB(0xffffff);
    [self.view addSubview:_segmentBackView];
    
    segBtnView = [[MySegButtonView alloc] initWithFrame:CGRectMake(0, 0, _segmentBackView.width, _segmentBackView.height )];
    segBtnView.titleArr = @[@"按等级统计",@"按竞争对手统计"];
    flag = 1;
    __unsafe_unretained SubDistrictController *subVC = self;
    __block NSMutableArray *arr = mineArray;
    segBtnView.block = ^(NSInteger btnTag , NSString * btnTitle){
        if ([btnTitle isEqualToString:@"按等级统计"]) {
            flag = 1;
        }else if ([btnTitle isEqualToString:@"按竞争对手统计"]){
            flag = 2;
        }
        [arr removeAllObjects];
         [subVC loadCityDataWithSeachText:nil];
    };
    [_segmentBackView addSubview:segBtnView];
    
//    _segment = [[UISegmentedControl alloc] initWithItems:@[@"按等级统计",@"按竞争对手统计"]];
//    _segment.frame = CGRectMake(30, 5, _segmentBackView.width - 60, _segmentBackView.height - 10);
//    _segment.hidden = NO;
////    _segment.backgroundColor = [UIColor cyanColor];
////    _segment.tintColor = GRAYCOLOR;
//    _segment.selectedSegmentIndex = 0;
//    flag = 1;
//    [_segment setTitleTextAttributes:@{NSForegroundColorAttributeName : BlueColor} forState:UIControlStateSelected];
//    [_segment setTioundColorAttributeName : BLACKCOLOR} forState:UIControlStateNormal];tleTextAttributes:@{NSForegr
//    [_segment setImage:[ViewTool getImageFromColor:[UIColor colorWithRed:223.0f / 255.0f green:233.0f / 255.0f blue:245.0 / 255.0f alpha:1] WithRect:CGRectMake(0, 0, _segment.width / 2.0f, _segment.height)] forSegmentAtIndex:0];
//    [_segment setImage:[ViewTool getImageFromColor:[UIColor whiteColor] WithRect:CGRectMake(0, 0, _segment.width / 2.0f, _segment.height)] forSegmentAtIndex:1];
//    _segment.layer.borderColor =[UIColor colorWithWhite:0.8 alpha:0.8].CGColor;
//    _segment.layer.cornerRadius = 5;
//    _segment.layer.borderWidth = 0.8;
//    _segment.tintColor = [UIColor colorWithRed:223.0f / 255.0f green:233.0f / 255.0f blue:245.0 / 255.0f alpha:1];
//    _segment.layer.shouldRasterize = YES;
//    _segment.backgroundColor = [UIColor whiteColor];
//     NSDictionary * selectedTextAttr = @{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:BlueColor};
//    [_segment setTitleTextAttributes:selectedTextAttr forState:UIControlStateSelected];
//    NSDictionary * unselectedTextAttr = @{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:GRAYCOLOR2};
//    [_segment setTitleTextAttributes:unselectedTextAttr forState:UIControlStateNormal];
//    [_segment addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
//    [_segmentBackView addSubview:_segment];
}

//创建头部视图
-(void)createSearchView
{
    CGFloat searchH = 60;
    _searchView = [[UIView alloc]initWithFrame:CGRectMake(0, btnView.maxY, kScreenWidth, searchH)];
    [_searchView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"Btn_Normal_Sousuodi.png"]]];
    _searchView.hidden = YES;
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 10, kScreenWidth - 40, searchH - 2*10)];
    imgView.image = [UIImage imageNamed:@"Btn_Normal_Sousuokuang.png"];
    [_searchView addSubview:imgView];
    
    [self.view addSubview:_searchView];
    
    _selectBtn = [self getButtonWithFrame:CGRectMake(imgView.x+5, imgView.y, 70, 40) withTitle:@"小区名称"];
    _selectBtn.tag = 0;
    [_selectBtn addTarget:self action:@selector(showSelectList:) forControlEvents:UIControlEventTouchUpInside];
    _selectBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [_selectBtn setTitleColor:UIColorFromRGB(0xa1a2a7) forState:UIControlStateNormal];
    _selectBtn.titleLabel.font = [ViewTool getFont:16.0f];
    [_searchView addSubview:_selectBtn];
    
    _selectListBtn = [ViewTool getButtonImgWithTarget:self withFrame:CGRectMake(_selectBtn.maxX, _selectBtn.y+12.5, 15, 15) WithString:@"Btn_Normal_Gengduo.png" WithAction:@selector(showSelectList:)];
    [_searchView addSubview:_selectListBtn];
    
    UIView *line = [ViewTool getLineViewWith:CGRectMake(_selectListBtn.maxX+5, _selectListBtn.y-2.5, 1, imgView.height-2*10) withBackgroudColor:LEFTLINECOLOR];
    [_searchView addSubview:line];
    
    _searchTF = [[UITextField alloc]initWithFrame:CGRectMake(line.x + 5, imgView.y , imgView.maxX - line.maxX -5, imgView.height)];
    _searchTF.delegate = self;
    _searchTF.placeholder = @"小区名称/城市/创建人";
    _searchTF.returnKeyType = UIReturnKeySearch;
    _searchTF.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [_searchTF setValue:LEFTLINECOLOR forKeyPath:@"_placeholderLabel.textColor"];
    [_searchTF setValue:[UIFont systemFontOfSize:16.0f] forKeyPath:@"_placeholderLabel.font"];
    [_searchView addSubview:_searchTF];
    
    [self addTaxisView];

}
- (void)addTaxisView{
    
    NSArray *titleArr = @[@"小区名称",@"城市",@"创建人"];
    CGFloat btnH = 40;
    CGFloat taxisW = 130;
    taxisView = [[UIView alloc]initWithFrame:CGRectMake(20, _searchView.maxY, taxisW, titleArr.count * btnH+10)];
    taxisView.hidden = YES;
    
    UIImageView *bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, taxisView.width, taxisView.height)];
    bgView.image = [UIImage imageNamed:@"Btn_Normal_Shaixuan.png"];
    [taxisView addSubview:bgView];
    [self.view addSubview:taxisView];
    
    for (int i= 0; i < titleArr.count; i++) {
        UIButton *btn = [self getButtonWithFrame:CGRectMake(10, 5+btnH*i, taxisView.width, btnH) withTitle:titleArr[i]];
        btn.tag = 112233 + i;
        [btn addTarget:self action:@selector(showSelectList:) forControlEvents:UIControlEventTouchUpInside];
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        btn.titleLabel.font = [ViewTool getFont:16.0f];
        [taxisView addSubview:btn];
        
        UIView *lineview = [ViewTool getLineViewWith:CGRectMake(0, 5+btnH*(i+1)-1, taxisView.width, 1) withBackgroudColor:DARKGRAYLINECOLOR];
        [taxisView addSubview:lineview];
        
        if (i == titleArr.count -1) {
            [lineview removeFromSuperview];
        }
    }
    
    btnTag = 112233;
    UIButton *btn = [taxisView viewWithTag:btnTag];
    [btn setTitleColor:YELLOWBTNCOLOR forState:UIControlStateNormal];
    
}
- (void)showSelectList:(UIButton *)sender{
    if (sender.tag == 0) {
        if (taxisView.hidden) {
            taxisView.hidden = NO;
        }else{
            taxisView.hidden = YES;
        }
    }else{
        UIButton *btn = [taxisView viewWithTag:btnTag];
        [btn setTitleColor:UIColorFromRGB(0xa1a2a7) forState:UIControlStateNormal];
        
        [sender setTitleColor:YELLOWBTNCOLOR forState:UIControlStateNormal];
        btnTag = (int)sender.tag;
        
        taxisView.hidden = YES;
        [_selectBtn setTitle:sender.currentTitle forState:UIControlStateNormal];
    }
    
}
- (UIButton *)getButtonWithFrame:(CGRect)frame withTitle:(NSString *)title {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:UIColorFromRGB(0xa1a2a7) forState:UIControlStateNormal];
    [button setTitleColor:YELLOWBTNCOLOR forState:UIControlStateHighlighted];
    button.titleLabel.font= [UIFont systemFontOfSize:16.0f];
    return button;
}
//xiugai
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    if(textField.text.length != 0){
        if (role == 1) {
            if (type == 2) {
                pageNum2 = 1;
                [expandArray removeAllObjects];
            }else if (type == 3){
                pageNum3 = 1;
                [talkingArray removeAllObjects];
            }
            [self loadCityDataWithSeachText:textField.text];
        }else if (role == 2){
            
            pageNum3 = 1;
            [expandArray removeAllObjects];
            [self loadAdminDataWithSeachText:textField.text];
        }else if(role == 0){
            if (_isChooseDistrict == YES) {
                type = 4;
                pageNum2 = 1;
                [expandArray removeAllObjects];
            }else{
                if (type == 1) {
                    pageNum1 = 1;
                    [mineArray removeAllObjects];
                }else if (type == 2 ){
                    pageNum2 = 1;
                    [expandArray removeAllObjects];
                }else{
//                    pageNum3 = 1;
//                    [talkingArray removeAllObjects];
                }
            }
            [self loadDataWithSearchText:textField.text];
        }
        
    }
    
    return YES;
}

//点击搜索框中的搜索
- (void)hideSearchButton
{
    if (_searchTF.text == nil || _searchTF.text.length == 0) {
        [Tools showMessage:@"请输入搜索关键字" view:self];
        return;
    }
    [_searchTF resignFirstResponder];
    [mineArray removeAllObjects];
    [self loadDataWithSearchText:_searchTF.text];
    
}

- (void)changeNoticeView:(NSInteger)tag
{
 
    for (int i = 0; i < btnView.btnTitleArray.count; i++) {
        TopBtn *btn = [btnView viewWithTag:9966 + i];
        if (tag == 9966 + i) {
            btn.lineView.backgroundColor = YELLOWBTNCOLOR;
        }else{
            btn.lineView.backgroundColor = BlueColor;
        }
    }
    
    
    if (role == 0) {
        //高级经理
        if (tag == 9966) {//我的小区
            type = 1;
            _newSubdistrictBtn.hidden = NO;
            righB.hidden = NO;

            if(mineArray.count == 0){
                 [self loadDataWithSearchText:nil];
            }
    }else if(tag == 9967){//公海小区
            type = 2;
            _newSubdistrictBtn.hidden = YES;
            righB.hidden = NO;
        if(expandArray.count == 0){
            [self loadDataWithSearchText:nil];
        }
        [_searchTF resignFirstResponder];
        
        }else{
            
        }
        righB.hidden = NO;
        if (isGet == YES) {
            _searchView.hidden = NO;
            _tableView.frame = CGRectMake(0, btnView.height + _searchView.height, kScreenWidth, kScreenHeight - btnView.height - 64 - _searchView.height);
            righB.selected = YES;
        }else{
            _searchView.hidden = YES;
            _tableView.frame = CGRectMake(0, btnView.height, kScreenWidth, kScreenHeight - btnView.height - 64);
            righB.selected = NO;
        }
        

    }else if(role == 1){//城市总监
        if (tag == 9966) {//小区统计的
            type = 1;
            _tableView.mj_header.hidden = YES;
            _tableView.mj_footer = nil;
            
             _newSubdistrictBtn.hidden = YES;
            _segmentBackView.hidden = NO;
            righB.hidden =YES;
            
            _searchView.hidden = YES;
            _tableView.frame = CGRectMake(0, _segmentBackView.height + btnView.height , kScreenWidth, kScreenHeight - _segmentBackView.height - btnView.height -64);
            
            if(mineArray.count == 0){
               [self loadCityDataWithSeachText:nil];
            }
            
        }else if(tag == 9967){//辖区小区
            type = 2;
            
            _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
                [self loadNewData];
            }];
            // 马上进入刷新状态
            //            [_tableView.mj_header beginRefreshing];
            
            _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                [self loadMoreData];
            }];
            
            _newSubdistrictBtn.hidden = NO;
            _segmentBackView.hidden = YES;
            
            righB.hidden =NO;
            if (isGet == YES) {
                _searchView.hidden = NO;
                _tableView.frame = CGRectMake(0, btnView.height + _searchView.height, kScreenWidth, kScreenHeight - btnView.height - 64 - _searchView.height);
                righB.selected = YES;
            }else{
                _searchView.hidden = YES;
                _tableView.frame = CGRectMake(0, btnView.height, kScreenWidth, kScreenHeight - btnView.height - 64);
                righB.selected = NO;
                
            }
            
            if(expandArray.count == 0){
                [self loadCityDataWithSeachText:nil];
            }
            
        }else{
            type = 3;
            
            _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
                [self loadNewData];
            }];
            // 马上进入刷新状态
            //            [_tableView.mj_header beginRefreshing];
            
            _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                [self loadMoreData];
            }];

            
            _newSubdistrictBtn.hidden = NO;
            _segmentBackView.hidden = YES;
            
            righB.hidden = NO;
            if (isGet == YES) {
                _searchView.hidden = NO;
                _tableView.frame = CGRectMake(0, btnView.height + _searchView.height, kScreenWidth, kScreenHeight - btnView.height - 64 - _searchView.height);
                righB.selected = YES;
            }else{
                _searchView.hidden = YES;
                _tableView.frame = CGRectMake(0, btnView.height, kScreenWidth, kScreenHeight - btnView.height - 64);
                righB.selected = NO;
                
            }
            if(talkingArray.count == 0){
                [self loadCityDataWithSeachText:nil];
            }
            [_searchTF resignFirstResponder];
        }
        
    }else if(role == 2){//管理者
        if (tag == 9966) {//小区统计
            type = 1;
            _tableView.mj_header.hidden = YES;
            _tableView.mj_footer = nil;
            
             _newSubdistrictBtn.hidden = YES;
            _segmentBackView.hidden = NO;
            _searchView.hidden = YES;
            _tableView.frame = CGRectMake(0, _segmentBackView.height + btnView.height , kScreenWidth, kScreenHeight - _segmentBackView.height - btnView.height -64);
            righB.hidden = YES;
            
            if(mineArray.count == 0){
               [self loadAdminDataWithSeachText:nil];
            }
        }else{//管辖小区
            type = 2;

            _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
                [self loadNewData];
            }];
            // 马上进入刷新状态
            //            [_tableView.mj_header beginRefreshing];
            
            _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                [self loadMoreData];
            }];
            _newSubdistrictBtn.hidden = YES;
            _segmentBackView.hidden = YES;
            
            righB.hidden = NO;
            if (isGet == YES) {
                _searchView.hidden = NO;
                _tableView.frame = CGRectMake(0, btnView.height + _searchView.height, kScreenWidth, kScreenHeight - btnView.height - 64 - _searchView.height);
                righB.selected = YES;
            }else{
                _searchView.hidden = YES;
                _tableView.frame = CGRectMake(0, btnView.height, kScreenWidth, kScreenHeight - btnView.height - 64);
                righB.selected = NO;
                
            }

            if(expandArray.count == 0){
            [self loadAdminDataWithSeachText:nil];
            }
        }
    }
    [_tableView reloadData];
    
}

#pragma mark --我的小区的新建按钮
- (void)addCreatbtn
{

    _newSubdistrictBtn = [ViewTool getAddButtonWithTarget:self WithString:@"add.png" WithAction:@selector(creatNew)];
    [self.view addSubview:_newSubdistrictBtn];
    
    if (role == 0) {
        _newSubdistrictBtn.hidden = NO;
    }else{
        _newSubdistrictBtn.hidden = YES;
    }
}
//创建小区
- (void)creatNew
{
//    CAKeyframeAnimation *ani = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
//    ani.values = @[@(M_PI_4),@(M_PI_2),@(2 * M_PI)];
//    
//    ani.delegate = self;
//    [_newSubdistrictBtn.layer addAnimation:ani forKey:nil];
    Admin_City_CreatDistricViewController * VC = [[Admin_City_CreatDistricViewController alloc] init];
    VC.delegate = self;
    if (role == 0) {
       
//        VC.responseMan = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
        VC.isBusiness = YES;
       
    }else{
        VC.isBusiness = NO;
       
    }
    VC.isChooseDistrict = _isChooseDistrict;
     [self.navigationController pushViewController:VC animated:NO];

}
#pragma mark --搜索框
- (void)clickSearch:(UIButton *)btn
{
    if (taxisView.hidden == NO) {
        taxisView.hidden = YES;
    }
    
    if (btn.selected == NO) {
        isGet = YES;
        _searchView.hidden = NO;
        _tableView.frame = CGRectMake(0, btnView.height + _searchView.height, kScreenWidth, kScreenHeight - btnView.height - 64 - _searchView.height);
        btn.selected =YES;
        
    }else{
        [_searchTF resignFirstResponder];
        isGet = NO;
        _searchView.hidden = YES;
        _searchTF.text = @"";
        _tableView.frame = CGRectMake(0, btnView.height, kScreenWidth, kScreenHeight - btnView.height - 64);
        btn.selected = NO;
    }
    
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (taxisView.hidden == NO) {
        taxisView.hidden = YES;
    }
}
- (void)viewDidDisappear:(BOOL)animated{
    if (taxisView.hidden == NO) {
        taxisView.hidden = YES;
    }
}
#pragma mark --tableview
- (void)addTableview
{
    CGFloat maxy;
    if(role == 1 || role == 2){
    if (type == 1) {
        maxy = _segmentBackView.height + btnView.height;
    }else if (type == 2){
        maxy = btnView.height;
    }
    }else{
        maxy = btnView.height;
    }
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, maxy, kScreenWidth, kScreenHeight - maxy - 64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = GrayBackColor;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    [self loadNewData];
    
    if (type == 4) {
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self loadNewData];
        }];
        // 马上进入刷新状态
        [_tableView.mj_header beginRefreshing];
        
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [self loadMoreData];
        }];
    }
    if (role == 0) {
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self loadNewData];
        }];
        // 马上进入刷新状态
        //        [_tableView.mj_header beginRefreshing];
        
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [self loadMoreData];
        }];
    }
    
}

- (void)loadNewData
{
    pageNum3 = 1;
    pageNum2 = 1;
    pageNum1 = 1;
    if(type == 1){
    [mineArray removeAllObjects];
    }
    if (type == 2 | type == 4) {
    [expandArray removeAllObjects];
    }
    if (type == 3) {
    [talkingArray removeAllObjects];
        
    }
   
    if (role == 0) {
         [self loadDataWithSearchText:_searchTF.text];
    }else if (role == 1){
        [self loadCityDataWithSeachText:_searchTF.text];
    }else if(role == 2){
        [self loadAdminDataWithSeachText:_searchTF.text];
    }
  
}

- (void)loadMoreData
{
    
    if (role == 0) {
        if (type == 1) {
            pageNum1 ++;
            [self loadDataWithSearchText:_searchTF.text];
        }else if (type == 2 | type == 4){
            pageNum2 ++;
            [self loadDataWithSearchText:_searchTF.text];
        }else{
        }
        
    }else if (role == 1){//城市总监
        if (type == 1) {
            
        }else if (type == 2){
            pageNum2 ++;
            [self loadCityDataWithSeachText:_searchTF.text];
        }else{
            pageNum3 ++;
            [self loadCityDataWithSeachText:_searchTF.text];
        }
        
    }else if(role == 2){
        if (type == 1) {
            
        }else if (type == 2){
            pageNum2 ++;
             [self loadAdminDataWithSeachText:_searchTF.text];
        }
   
    }
}
#pragma mark --请求数据
- (void)loadAdminDataWithSeachText:(NSString *)searchText
{
    if (type == 1) {//请求统计小区数据
        NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:@{@"method":@"app.village.count.all",@"flag" :@(flag)}];//统计标示符：1 等级统计 2 竞争统计
        [mineArray removeAllObjects];
        NSDictionary *paramersDict = [DataTool addSignToDic:dict];
        [DataTool postWithUrl:HEAD_URL parameters:paramersDict success:^(id data) {
            id backData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments | NSJSONReadingMutableContainers error:nil];
            NSLog(@"小区统计的数据%@",backData);
            if ([backData[@"status"] isEqualToString:@"SUCCESS"]) {
                NSArray *dataArray = backData[@"data"];
                
                for (int i = 0; i< dataArray.count; i++) {
                    StatisticModel *model = [[StatisticModel alloc] init];
                    [model setValuesForKeysWithDictionary:dataArray[i]];
                    [mineArray addObject:model];
                }
                [_tableView reloadData];
                if(dataArray.count == 0){
                    [Tools showMessage:@"暂无数据" view:self];
                }
            }else{
                [Tools showMessage:backData[@"msg"] view:self];
            }
        } fail:^(NSError *error) {
            NSLog(@"%@",error.localizedDescription);
            [Tools showMessage:error.localizedDescription view:self];
        }];
    }else if(type == 2){//获取所有小区的数据
        NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:@{@"method":@"app.village.manage.all",@"page" :@(pageNum2)}];
        
        if (searchText.length != 0 ) {
            NSString *keyStr ;
            NSInteger tag  = btnTag - 112233;
            if (tag == 0) {
                keyStr = @"title";
            }else if (tag == 1){
                keyStr = @"city";
            }else{
                keyStr = @"create_user";
            }
            [dict setObject:searchText forKey:keyStr];
        }
        NSDictionary *paramersDict = [DataTool addSignToDic:dict];
        [DataTool postWithUrl:HEAD_URL parameters:paramersDict success:^(id data) {
            id backData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments | NSJSONReadingMutableContainers error:nil];
            NSLog(@"所有小区的数据%@",backData);
            if ([backData[@"status"] isEqualToString:@"SUCCESS"]) {
                NSArray *dataArray = backData[@"data"][@"listData"];
                
                for (int i = 0; i< dataArray.count; i++) {
                    MIneDistrictModel *model = [[MIneDistrictModel alloc] init];
                    [model setValuesForKeysWithDictionary:dataArray[i]];
                    [expandArray addObject:model];
                }
                
                [_tableView reloadData];
                [_tableView.mj_header endRefreshing];
                
                int total = [backData[@"data"][@"total"] intValue];
                if(dataArray.count == 0){
                    [Tools showMessage:@"暂无数据" view:self];
                }
                
                if (total <= 10) {
                    [_tableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    if (dataArray.count < 10) {
                        [_tableView.mj_footer endRefreshingWithNoMoreData];
                    }else{
                        [_tableView.mj_footer endRefreshing];
                    }
                }
                
            }else{
                [Tools showMessage:backData[@"msg"] view:self];
                
                [_tableView.mj_header endRefreshing];
                [_tableView.mj_footer endRefreshing];
            }
            
        } fail:^(NSError *error) {
            NSLog(@"%@",error.localizedDescription);
            [_tableView.mj_header endRefreshing];
            [_tableView.mj_footer endRefreshing];
            [Tools showMessage:error.localizedDescription view:self];
        }];
    }

}
//城市总监
- (void)loadCityDataWithSeachText:(NSString *)searchText
{
    if (type == 1) {//请求tongji数据
        
        NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:@{@"method":@"app.village.count.all",@"flag" :@(flag)}];//统计标示符：1 等级统计 2 竞争统计
        [mineArray removeAllObjects];
        NSDictionary *paramersDict = [DataTool addSignToDic:dict];
        [DataTool postWithUrl:HEAD_URL parameters:paramersDict success:^(id data) {
            id backData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments | NSJSONReadingMutableContainers error:nil];
            NSLog(@"小区统计的数据%@",backData);
            if ([backData[@"status"] isEqualToString:@"SUCCESS"]) {
                NSArray *dataArray = backData[@"data"];
                
                for (int i = 0; i< dataArray.count; i++) {
                    StatisticModel *model = [[StatisticModel alloc] init];
                    [model setValuesForKeysWithDictionary:dataArray[i]];
                    [mineArray addObject:model];
                }
                [_tableView reloadData];
                if(dataArray.count == 0){
                    [Tools showMessage:@"暂无数据" view:self];
                }
            }else{
                [Tools showMessage:backData[@"msg"] view:self];
            }
         
        } fail:^(NSError *error) {
            NSLog(@"%@",error.localizedDescription);
            [Tools showMessage:error.localizedDescription view:self];
        }];

    }else if(type == 2){//获取辖区小区的数据
        NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:@{@"method":@"app.village.agency.area",@"page" :@(pageNum2)}];
      
        if (searchText.length != 0 ) {
            NSString *keyStr ;
            NSInteger tag  = btnTag - 112233;
            if (tag == 0) {
                keyStr = @"title";
            }else if (tag == 1){
                keyStr = @"city";
            }else{
                keyStr = @"create_user";
            }
            [dict setObject:searchText forKey:keyStr];
        }
        NSDictionary *paramersDict = [DataTool addSignToDic:dict];
        NSLog(@"辖区小区参数*-*-*-*-*-*");
        [DataTool postWithUrl:HEAD_URL parameters:paramersDict success:^(id data) {
            id backData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments | NSJSONReadingMutableContainers error:nil];
            NSLog(@"辖区小区的数据%@",backData);
            if ([backData[@"status"] isEqualToString:@"SUCCESS"]) {
                
                NSArray *dataArray = backData[@"data"][@"listData"];
                
                for (int i = 0; i< dataArray.count; i++) {
                    MIneDistrictModel *model = [[MIneDistrictModel alloc] init];
                    [model setValuesForKeysWithDictionary:dataArray[i]];
                    [expandArray addObject:model];
                    
                }
                [_tableView reloadData];
                [_tableView.mj_header endRefreshing];
                int total = [backData[@"data"][@"total"] intValue];
                if(dataArray.count == 0){
                    [Tools showMessage:@"暂无数据" view:self];
                }
                if (total <= 10) {
                    [_tableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    if (dataArray.count < 10) {
                        [_tableView.mj_footer endRefreshingWithNoMoreData];
                    }else{
                        [_tableView.mj_footer endRefreshing];
                    }
                }
            }else{
                [Tools showMessage:backData[@"msg"] view:self];
                
                [_tableView.mj_header endRefreshing];
                [_tableView.mj_footer endRefreshing];
            }

        } fail:^(NSError *error) {
            NSLog(@"%@",error.localizedDescription);
            [_tableView.mj_header endRefreshing];
               [_tableView.mj_footer endRefreshing];
            [Tools showMessage:error.localizedDescription view:self];
        }];
        
    }else if(type == 3){
        NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:@{@"method":@"app.village.business.talk",@"page" :@(pageNum3)}];
        if (searchText.length != 0 ) {
            NSString *keyStr ;
            NSInteger tag  = btnTag - 112233;
            if (tag == 0) {
                keyStr = @"title";
            }else if (tag == 1){
                keyStr = @"city";
            }else{
                keyStr = @"create_user";
            }
            [dict setObject:searchText forKey:keyStr];
        }
        NSDictionary *paramersDict = [DataTool addSignToDic:dict];
        [DataTool postWithUrl:HEAD_URL parameters:paramersDict success:^(id data) {
            id backData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments | NSJSONReadingMutableContainers error:nil];
            NSLog(@"在谈小区的数据%@",backData);
            if ([backData[@"status"] isEqualToString:@"SUCCESS"]) {
                NSArray *dataArray = backData[@"data"][@"listData"];
                
                for (int i = 0; i< dataArray.count; i++) {
                    MIneDistrictModel *model = [[MIneDistrictModel alloc] init];
                    [model setValuesForKeysWithDictionary:dataArray[i]];
                    [talkingArray addObject:model];
                    
                }
                [_tableView reloadData];
                [_tableView.mj_header endRefreshing];
                int total = [backData[@"data"][@"total"] intValue];
                if(dataArray.count == 0){
                    [Tools showMessage:@"暂无数据" view:self];
                }
                if (total <= 10) {
                    [_tableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    if (dataArray.count < 10) {
                        [_tableView.mj_footer endRefreshingWithNoMoreData];
                    }else{
                        [_tableView.mj_footer endRefreshing];
                    }
                }
            }else{
                [Tools showMessage:backData[@"msg"] view:self];
                
                [_tableView.mj_header endRefreshing];
                [_tableView.mj_footer endRefreshing];
            }
            
        } fail:^(NSError *error) {
            NSLog(@"%@",error.localizedDescription);
            [Tools showMessage:error.localizedDescription view:self];
            [_tableView.mj_header endRefreshing];
            [_tableView.mj_footer endRefreshing];
        }];
    }

}
- (void)loadDataWithSearchText:(NSString *)searchText
{
        if (type == 1) {//请求我的小区数据
            NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:@{@"method":@"app.village.business.my",@"page" :@(pageNum1)}];
            if (searchText.length != 0 ) {
                NSString *keyStr ;
                NSInteger tag  = btnTag - 112233;
                if (tag == 0) {
                    keyStr = @"title";
                }else if (tag == 1){
                    keyStr = @"city";
                }else{
                    keyStr = @"create_user";
                }
                [dict setObject:searchText forKey:keyStr];
            }

            NSDictionary *paramersDict = [DataTool addSignToDic:dict];
            [DataTool postWithUrl:HEAD_URL parameters:paramersDict success:^(id data) {
                id backData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments | NSJSONReadingMutableContainers error:nil];
                NSLog(@"我的小区的数据%@",backData);
                if ([backData[@"status"] isEqualToString:@"SUCCESS"]) {

                    NSArray *dataArray = backData[@"data"][@"listData"];
                    
                    for (int i = 0; i< dataArray.count; i++) {
                        MIneDistrictModel *model = [[MIneDistrictModel alloc] init];
                        [model setValuesForKeysWithDictionary:dataArray[i]];
                        [mineArray addObject:model];
                        
                    }
                    [_tableView reloadData];
                    [_tableView.mj_header endRefreshing];
                    
                    int total = [backData[@"data"][@"total"] intValue];
                    if(dataArray.count == 0){
                        [Tools showMessage:@"暂无数据" view:self];
                    }
                    if (total <= 10) {
                        [_tableView.mj_footer endRefreshingWithNoMoreData];
                    }else{
                        if (dataArray.count < 10) {
                            [_tableView.mj_footer endRefreshingWithNoMoreData];
                        }else{
                            [_tableView.mj_footer endRefreshing];
                        }
                    }
                    
                }else{
                    [Tools showMessage:backData[@"msg"] view:self];
                    
                    [_tableView.mj_header endRefreshing];
                    [_tableView.mj_footer endRefreshing];
                }
            } fail:^(NSError *error) {
                NSLog(@"%@",error.localizedDescription);
                [Tools showMessage:error.localizedDescription view:self];
                [_tableView.mj_header endRefreshing];
                [_tableView.mj_footer endRefreshing];
            }];
 
        }else if(type == 2 | type == 4){//获取拓展小区的数据
            NSMutableDictionary *dict;
            if(type == 2){
                dict = [NSMutableDictionary dictionaryWithDictionary:@{@"method":@"app.village.business.expand",@"page" :@(pageNum2)}];
            }else if (type == 4){
                dict = [NSMutableDictionary dictionaryWithDictionary:@{@"method":@"app.village.business.visit",@"page" :@(pageNum2)}];
            }
            if (searchText.length != 0 ) {
                
                NSString *keyStr ;
                NSInteger tag  = btnTag - 112233;
                if (tag == 0) {
                    keyStr = @"title";
                }else if (tag == 1){
                    keyStr = @"city";
                }else{
                    keyStr = @"create_user";
                }
                [dict setObject:searchText forKey:keyStr];
            }
            
            if (type == 4){
                NSLog(@"_isTongQian:%d",_isTongQian);
                if (_isTongQian) {
                    [dict setObject:@(1) forKey:@"is_merge"];//统签
                }else{
                    [dict setObject:@(0) forKey:@"is_merge"];//非统签
                }
            }

            NSDictionary *paramersDict = [DataTool addSignToDic:dict];
            [DataTool postWithUrl:HEAD_URL parameters:paramersDict success:^(id data) {
                id backData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments | NSJSONReadingMutableContainers error:nil];
                NSLog(@"拓展小区的数据%@",backData);
                if ([backData[@"status"] isEqualToString:@"SUCCESS"]) {
                    NSArray *dataArray = backData[@"data"][@"listData"];
                    
                    for (int i = 0; i< dataArray.count; i++) {
                        MIneDistrictModel *model = [[MIneDistrictModel alloc] init];
                        [model setValuesForKeysWithDictionary:dataArray[i]];
                        [expandArray addObject:model];
                        
                    }
                    [_tableView reloadData];
                    [_tableView.mj_header endRefreshing];
                    
                    int total = [backData[@"data"][@"total"] intValue];
                    if(dataArray.count == 0){
                        [Tools showMessage:@"暂无数据" view:self];
                    }
                    if (total <= 10) {
                        [_tableView.mj_footer endRefreshingWithNoMoreData];
                    }else{
                        if (dataArray.count < 10) {
                            [_tableView.mj_footer endRefreshingWithNoMoreData];
                        }else{
                            [_tableView.mj_footer endRefreshing];
                        }
                    }
                    
                }else{
                    [Tools showMessage:backData[@"msg"] view:self];
                    
                    [_tableView.mj_header endRefreshing];
                    [_tableView.mj_footer endRefreshing];
                }

                } fail:^(NSError *error) {
                NSLog(@"%@",error.localizedDescription);
                [Tools showMessage:error.localizedDescription view:self];
                [_tableView.mj_header endRefreshing];
                [_tableView.mj_footer endRefreshing];
            }];

        }
}

#pragma mark --tableview代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        if (type == 1) {
            return mineArray.count;
        }else if (type == 2 | type == 4){
            NSLog(@"%lu",(unsigned long)expandArray.count);
            return expandArray.count;
        }else if(type == 3){
            return talkingArray.count;
        }
    return 0;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(role == 0){
        if (type == 1) {
            SubDsitrictCell *cell = [SubDsitrictCell cellWithTabelView:tableView];
            if(mineArray.count != 0 ){
            cell.model = mineArray[indexPath.row];
            }
            cell.backgroundColor = GrayBackColor;
            return cell;
        }else if (type == 4){
            SelectDistrictCell *cell = [SelectDistrictCell cellWithTabelView:tableView];
            
            if (expandArray.count != 0 ) {
                cell.model = expandArray[indexPath.row];
            }
            cell.backgroundColor = GrayBackColor;
            return cell;
        }else if(type == 2){
            ExpandTalkingVillageCell *cell = [ExpandTalkingVillageCell cellWithTableView:tableView];
            
//                if (type == 2) {
                    if (expandArray.count != 0 ) {
                    cell.model = expandArray[indexPath.row];
                    }
//                }else if (type == 3){
//                    if (talkingArray.count != 0) {
//                        cell.model = talkingArray[indexPath.row];
//                    }
//                }
        
            cell.backgroundColor = GrayBackColor;
            return cell;
        }
    }else {
        if (type == 1) {
            SubDistrictStatisticCell *cell = [SubDistrictStatisticCell cellWithTabelView:tableView];
            cell.numLabel.text = [NSString stringWithFormat:@"%03d",(int)indexPath.row + 1];
            if (mineArray.count != 0 ) {
                cell.model = mineArray[indexPath.row];
            }
            if (flag == 1) {
                cell.levelLable.text = @"等级";
            }else{
               cell.levelLable.text = @"竞争对手";
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }else if(type == 2){
         AdministerDistrictCell *cell = [AdministerDistrictCell cellWithTabelView:tableView];
//            _newSubdistrictBtn.hidden = NO;
            if (expandArray.count != 0) {
                cell.model = expandArray[indexPath.row];
            }
            cell.backgroundColor = GrayBackColor;
            return cell;
        }else{
            ExpandTalkingVillageCell *cell = [ExpandTalkingVillageCell cellWithTableView:tableView];
            if (talkingArray.count != 0) {
                cell.model = talkingArray[indexPath.row];
            }
            cell.backgroundColor = GrayBackColor;
            return cell;
        }
        
    }
//        else{
//        if (_clickbtn.tag == 9966) {
//            SubDistrictStatisticCell *cell = [SubDistrictStatisticCell cellWithTabelView:tableView];
//            //            _newSubdistrictBtn.hidden = YES;
//            cell.numLabel.text = [NSString stringWithFormat:@"%03d",(int)indexPath.row];
//            //            cell.backgroundColor = GrayBackColor;
//            return cell;
//        }else {
//            AdministerDistrictCell *cell = [AdministerDistrictCell cellWithTabelView:tableView];
//            //            _newSubdistrictBtn.hidden = NO;
//            NSLog(@"expand个数  %ld",expandArray.count);
//            cell.model = expandArray[indexPath.row];
//            cell.backgroundColor = GrayBackColor;
//            return cell;
//        }
//    }
   
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
      MIneDistrictModel *model;
    if (self.isChooseDistrict == NO) {
        SubDistrictDetailController *detailVC = [[SubDistrictDetailController alloc] init];
//        MIneDistrictModel *model;
        if(role == 0){
            if (type == 1) {
                if (mineArray.count >0) {
                    model = mineArray[indexPath.row];
                }
                
            }else if (type == 2){
                if (expandArray.count >0) {
                    model = expandArray[indexPath.row];
                }
                //
            }else{
                if (talkingArray.count > 0) {
                    model = talkingArray[indexPath.row];
                }
            }
            detailVC.districtID = model.districtId;
            NSLog(@"小区的id%@",model.districtId);
            [self.navigationController pushViewController:detailVC animated:YES];
        }else if(role == 1){
            if (type == 1) {
                
            }else{//城市总监的辖区小区
                if (expandArray.count >0) {
                    model = expandArray[indexPath.row];
                    detailVC.districtID = model.districtId;
                }
                NSLog(@"小区的id%@",model.districtId);
                [self.navigationController pushViewController:detailVC animated:YES];
            }
            
        }else if(role == 2){
            if (type == 1) {
              
            }else{//管理者的所有小区
                
                if (expandArray.count >0) {
                    model = expandArray[indexPath.row];
                    detailVC.districtID = model.districtId;
                }
                NSLog(@"小区的id%@",model.districtId);
                [self.navigationController pushViewController:detailVC animated:YES];
            }
            
        }
    }else{
        if ([self.delgate respondsToSelector:@selector(chooseDistrict:)]) {
            
            
            if(role == 0){
                if (type == 1) {
                    model = mineArray[indexPath.row];
                    
                }else if (type == 4){
                    model = expandArray[indexPath.row];
                    
                    //
                }else{
                    model = talkingArray[indexPath.row];
                }
                
                if (model.is_visit == 1) {
                    [Tools showMessage:@"该小区已拜访，请选择其他小区" view:self];
                }else{
                NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:@{@"method":@"app.village.detail",@"id" : model.districtId}];
                NSDictionary *paramesDict = [DataTool addSignToDic:dict];
                [DataTool postWithUrl:HEAD_URL parameters:paramesDict success:^(id data) {
                    id backData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments | NSJSONReadingMutableContainers error:nil];
                    NSLog(@"小区详情的数据:%@",backData);
                    NSDictionary *dict = backData[@"data"];
                    SubDistrictDetailModel *m = [[SubDistrictDetailModel alloc] init];
                    [m setValuesForKeysWithDictionary:dict];
                     [self.delgate chooseDistrict:m];
                 
                } fail:^(NSError *error) {
                    NSLog(@"%@",error.localizedDescription);
                }];
                }
            }else if(role == 1){
                if (type == 1) {
                    return;
                }else{
                    
                    model = expandArray[indexPath.row];
                    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:@{@"method":@"app.village.detail",@"id" : model.districtId}];
                    NSDictionary *paramesDict = [DataTool addSignToDic:dict];
                    [DataTool postWithUrl:HEAD_URL parameters:paramesDict success:^(id data) {
                        id backData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments | NSJSONReadingMutableContainers error:nil];
                        NSLog(@"小区详情的数据:%@",backData);
                        NSDictionary *dict = backData[@"data"];
                        SubDistrictDetailModel *m = [[SubDistrictDetailModel alloc] init];
                        [m setValuesForKeysWithDictionary:dict];
                        [self.delgate chooseDistrict:m];
                        
                    } fail:^(NSError *error) {
                        NSLog(@"%@",error.localizedDescription);
                    }];

                }
                
            }else if(role == 2){
                if (type == 1) {
                    return;
                }else{
                    model = expandArray[indexPath.row];
                    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:@{@"method":@"app.village.detail",@"id" : model.districtId}];
                    NSDictionary *paramesDict = [DataTool addSignToDic:dict];
                    [DataTool postWithUrl:HEAD_URL parameters:paramesDict success:^(id data) {
                        id backData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments | NSJSONReadingMutableContainers error:nil];
                        NSLog(@"小区详情的数据:%@",backData);
                        NSDictionary *dict = backData[@"data"];
                        SubDistrictDetailModel *m = [[SubDistrictDetailModel alloc] init];
                        [m setValuesForKeysWithDictionary:dict];
                        [self.delgate chooseDistrict:m];
                        
                    } fail:^(NSError *error) {
                        NSLog(@"%@",error.localizedDescription);
                    }];

                }
                
            }

            
        }
        
        [self goToBack];
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(role == 0){
        if (type == 1) {
            return [SubDsitrictCell cellHeight];
        }else if (type == 4){
            return [SelectDistrictCell cellHeight];
        }else{
            return [ExpandTalkingVillageCell cellHeight];
        }
 
    }else {
        if (type == 1) {
            return [SubDistrictStatisticCell cellHeight];
        }else if(type == 2){
            return [AdministerDistrictCell cellHeight];
        }else{
            return [ExpandTalkingVillageCell cellHeight];
        }
    }
    return 0;
}
//AddVisitViewController@protocol UIViewPassValueDelegate
- (void)passValue:(NSString *)value{
    NSLog(@"passValue:%@",value);
    if ([value isEqualToString:@"SUCCESS"]) {
        NSLog(@"^&676");
        [_tableView.mj_header beginRefreshing];
    }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    if (taxisView.hidden == NO) {
        taxisView.hidden =YES;
    }
    
}
// 拉开左侧:点击.
- (void)sideLeftButton_selector:(id)sender {
    [self.revealController toggleSidebar:!self.revealController.sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
}

- (void)goToBack{
    [self.navigationController popViewControllerAnimated:YES];
}
@end
